#!/usr/local/bin/perl
package HELP;
use strict; use warnings;
no if $] >= 5.018, warnings => "experimental::smartmatch";
use feature qw/switch/;

use Glib qw/TRUE FALSE/;
use Gtk2;
use Gtk2::Gdk::Keysyms;

my $win;
my $nb;
my $maximized=0;
my $above=0;

sub key_press_event
{
    my ($w,$e) = @_;
    my $s=($e->get_state >= ['shift-mask'])?1:0;
    my $c=($e->get_state >= ['control-mask'])?1:0;
    my $m=($e->get_state >= ['mod1-mask'])?1:0;
    given ( $e->keyval ){
	when ( [$Gtk2::Gdk::Keysyms{Escape},
		$Gtk2::Gdk::Keysyms{q}]){
	    $w->destroy;
	}
	when($Gtk2::Gdk::Keysyms{Down}){
	    &scroll("v",1,$s?2:1)
	}
	when($Gtk2::Gdk::Keysyms{Up}){
	    &scroll("v",-1,$s?2:1)
	}
	when($Gtk2::Gdk::Keysyms{Right}){
	    &scroll("h",1,$s?2:1)
	}
	when($Gtk2::Gdk::Keysyms{Left}){
	    &scroll("h",-1,$s?2:1)
	}
	when($Gtk2::Gdk::Keysyms{space}){
	    &scroll("v",$s?-1:1,3)
	}
	when($Gtk2::Gdk::Keysyms{Tab}){
	    unless($s){
		&to_next(1);
	    }else{
		&to_next(-1);
	    }
	}
	when($Gtk2::Gdk::Keysyms{Return}){
	    &to_next(1);
	}
	when($Gtk2::Gdk::Keysyms{BackSpace}){
	    &to_next(-1);
	}
	when ( $Gtk2::Gdk::Keysyms{M} ){
	    my $m=$maximized;
	    $maximized?$win->unmaximize:$win->maximize;
	    $maximized=!$maximized if $maximized==$m; # <<< ws failed
	}
	when ( $Gtk2::Gdk::Keysyms{F} ){
	    my $a=$above;
	    $win->set_keep_above($above?FALSE:TRUE);
	    $above=!$above if $a==$above; # ws_event failed
	}
	when ( $Gtk2::Gdk::Keysyms{z} ){
	    $win->iconify if $c;
	}
	default{return FALSE;}
    }
    return TRUE;
}

sub ws_event
{
    my ($w,$e) = @_;
    foreach("maximized","fullscreen","above"){
	if ( $e->changed_mask & $_ ){
	    no strict "refs";
	    if ( $e->new_window_state & $_ ){
		$$_ = 1;
	    }else{
		$$_ = 0;
	    }
	}
    }
}

sub to_next
{
    if($_[0]>0){
	if($nb->get_current_page+1==$nb->get_n_pages){
	    $nb->set_current_page(0);
	}else{
	    $nb->next_page;
	}
    }else{
	if($nb->get_current_page==0){
	    $nb->set_current_page($nb->get_n_pages-1);
	}else{
	    $nb->prev_page;
	}
    }
}

sub scroll
{
# 0: h=horizontal, v=vertical
# 1: +=increment, -=decriment
# 2: 0=little, 1=small, 2=large
    my ($hv,$d,$x)=@_;
    my $k=$nb->get_current_page;
    my $s=($nb->get_children)[$k];
    my $f="get_".$hv."adjustment";
    my $a=$s->$f;
    my $dx;
    my $v=$a->value;
    given($x){
	when(1){
	    $dx=$a->step_increment/2; # <<<<
	}
	when(2){
	    $dx=$a->step_increment;
	}
	when(3){
	    $dx=$a->page_increment;
	}
    }
    $dx=-$dx if $d < 0;
    $v+=$dx;
    $a->set_value($v);
}

sub show_help
{
    $win = Gtk2::Window->new;
    $nb=Gtk2::Notebook->new;
    our ($sw_sys,$L_sys);
    our ($sw_fio,$L_fio);
    our ($sw_nav,$L_nav);
    our ($sw_adj,$L_adj);
    our ($sw_mpl,$L_mpl);
    our ($sw_ent,$L_ent);
    our ($sw_srt,$L_srt);
    our ($sw_tnv,$L_tnv);
    our ($sw_pop,$L_pop);
    our ($sw_ply,$L_ply);
    our ($sw_shc,$L_shc);
    our ($sw_nkc,$L_nkc);
    our ($sw_msc,$L_msc);
    our ($sw_mou,$L_mou);
    our ($sw_com,$L_com);

    $win->signal_connect(key_press_event => \&key_press_event);
    $win->signal_connect(window_state_event => \&ws_event);
    $win->set_border_width(4);
    $win->set_title("Key-binds: ".$_[1]);
    $win->set_default_size($_[2],$_[3]);
    $win->set_icon_list(@_[8..$#_]);

    my $d="$_[0]/docs";
    foreach("sys","fio","nav","adj","mpl","ent","srt","tnv","pop","ply","shc","nkc","msc","mou","com"){
	my $t;
	if(open my $fh, "<","$d/kb-$_.txt"){
	    $t=do{local $/;<$fh>;};
	    close $fh;
	}else{
	    $t="\nDocument not found:\n\t$d/kb-$_.txt";
	}
	my $L="L_".$_;
	my $sw="sw_".$_;
	{
	    no strict "refs";
	    $$sw=Gtk2::ScrolledWindow->new;
	    $$sw->set_policy("automatic","automatic");
	    $$L=Gtk2::Label->new;
	    $$L->set_text("\n".$t);
	    $$sw->add_with_viewport($$L);
	}
    }

    my $uc="[User defined commands]\n"
	."\nC-e = ".(defined($_[4])?$_[4]:"undef")
	."\nC-m = ".(defined($_[5])?$_[5]:"undef")
	."\nC-j = ".(defined($_[6])?$_[6]:"undef");
    my $precs=$_[7];
    my $k=0;
    foreach(@$precs){
	$k++;
	if(defined $_){
	    $uc.="\nF$k = $_";
	}
    }
    $L_com->set_text($uc);

    $nb->append_page($sw_sys,"Sys ");
    $nb->append_page($sw_fio,"File ");
    $nb->append_page($sw_nav,"Navi ");
    $nb->append_page($sw_adj,"Adj ");
    $nb->append_page($sw_mpl,"Image ");
    $nb->append_page($sw_ent,"Entry ");
    $nb->append_page($sw_srt,"Sorter ");
    $nb->append_page($sw_tnv,"Thumb ");
    $nb->append_page($sw_pop,"Pop ");
    $nb->append_page($sw_ply,"Player ");
    $nb->append_page($sw_shc,"Shell ");
    $nb->append_page($sw_nkc,"NKCom ");
    $nb->append_page($sw_msc,"Misc ");
    $nb->append_page($sw_mou,"Mouse ");
    $nb->append_page($sw_com,"User ");

    $win->add($nb);
    $win->show_all;
}

1;
